package com.feng.common.util;

import com.feng.common.enums.AlgorithmEnum;
import com.feng.common.exception.ErrorCode;
import com.feng.common.exception.ValidationException;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.util.ByteSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;

/**
 * @author Lee
 */
public class MD5Util {

    private final static Logger LOGGER = LoggerFactory.getLogger(MD5Util.class);

    /**
     * hash次数
     */
    private static final Integer HASH_ITERATION = 1024;

    public static SimpleHash toMd5(AlgorithmEnum algorithm, Object content, Object salt, Integer hashIterations){
        if (StringUtils.isEmpty(content)) {
            LOGGER.error("加密内容为空");
            throw new ValidationException(ErrorCode.CheckError,"加密内容为空");
        }
        ByteSource byteSourceSalt = ByteSource.Util.bytes(salt);
        return new SimpleHash(algorithm.toString(),content,byteSourceSalt,hashIterations);
    }

    public static SimpleHash toMd5(AlgorithmEnum algorithm, Object content, String salt){
        return toMd5(algorithm,content,salt,HASH_ITERATION);
    }

}
